package com.azure.code.bfs;

import com.azure.code.tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 二叉树的最小深度
 * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
 */
public class MinDepthBTree_111 {

    public int minDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int dept = 1;
        while (!queue.isEmpty()) {
            int sz = queue.size();
            for (int i = 0; i < sz; i++) {
                TreeNode cur = queue.poll();
                if(cur.left==null&&cur.right==null) {
                    return dept;
                }
                if(cur.left!=null)
                queue.offer(cur.left);
                if(cur.right!=null)
                    queue.offer(cur.right);
            }
            dept++;
        }
        return dept;
    }
}

